1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Data;
5 using
System.Drawing;
6 using
System.Linq;
7 using
System.Text;
8 using
System.Windows.Forms;
9 using
System.Data.SqlClient;
10 using
Excel = Microsoft.Office.Interop.Excel;
11 using
System.IO;
12 namespace
WarehouseManagementSystem
13 {
14     
public partial class frmProductsRecord : Form
15     {
16        SqlDataReader rdr =
null;
17        SqlConnection con =
null;
18        SqlCommand cmd =
null;
19        ConnectionString cs =
new ConnectionString();
20         
public frmProductsRecord()
21         {
22             InitializeComponent();
23         }
24         
public void GetData()
25         {
26             
try
27             {
28                 con =
new SqlConnection(cs.DBConn);
29                 con.Open();
30                 cmd =
new SqlCommand("SELECT RTRIM(ProductID),RTRIM(ProductName),RTRIM(Category.ID),RTRIM(CategoryName),RTRIM(SubCategory.ID),RTRIM(SubCategoryName),RTRIM(Features),RTRIM(Price),Image from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID order by Productname", con);
31                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
32                 dataGridView1.Rows.Clear();
33                 
while (rdr.Read() == true)
34                 {
35                     dataGridView1.Rows.Add(rdr[
0],rdr[1],rdr[2],rdr[3],rdr[4],rdr[5],rdr[6],rdr[7],rdr[8]);
36                 }
37                 con.Close();
38             }
39             
catch (Exception ex)
40             {
41                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
42             }
43         }
44         
private void frmProductsRecord_Load(object sender, EventArgs e)
45         {
46             GetData();
47         }
48
49        
50         
private void Button3_Click(object sender, EventArgs e)
51         {
52             
int rowsTotal = 0;
53             
int colsTotal = 0;
54             
int I = 0;
55             
int j = 0;
56             
int iC = 0;
57             System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
58             Excel.Application xlApp =
new Excel.Application();
59
60             
try
61             {
62                 Excel.Workbook excelBook = xlApp.Workbooks.Add();
63                 Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelBook.Worksheets[
1];
64                 xlApp.Visible =
true;
65
66                 rowsTotal = dataGridView1.RowCount;
67                 colsTotal = dataGridView1.Columns.Count -
1;
68                 
var _with1 = excelWorksheet;
69                 _with1.Cells.Select();
70                 _with1.Cells.Delete();
71                 
for (iC = 0; iC <= colsTotal; iC++)
72                 {
73                     _with1.Cells[
1, iC + 1].Value = dataGridView1.Columns[iC].HeaderText;
74                 }
75                 
for (I = 0; I <= rowsTotal - 1; I++)
76                 {
77                     
for (j = 0; j <= colsTotal; j++)
78                     {
79                         _with1.Cells[I +
2, j + 1].value = dataGridView1.Rows[I].Cells[j].Value;
80                     }
81                 }
82                 _with1.Rows[
"1:1"].Font.FontStyle = "Bold";
83                 _with1.Rows[
"1:1"].Font.Size = 12;
84
85                 _with1.Cells.Columns.AutoFit();
86                 _with1.Cells.Select();
87                 _with1.Cells.EntireColumn.AutoFit();
88                 _with1.Cells[
1, 1].Select();
89             }
90             
catch (Exception ex)
91             {
92                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
93             }
94             
finally
95             {
96                 
//RELEASE ALLOACTED RESOURCES
97                 System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
98                 xlApp =
null;
99             }
100         }
101
102         
private void frmProductsRecord_FormClosing(object sender, FormClosingEventArgs e)
103         {
104             
this.Hide();
105             frmProduct frm =
new frmProduct();
106             frm.Show();
107         }
108
109         
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
110         {
111             
string strRowNumber = (e.RowIndex + 1).ToString();
112             SizeF size = e.Graphics.MeasureString(strRowNumber,
this.Font);
113             
if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
114             {
115                 dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width +
20));
116             }
117             Brush b = SystemBrushes.ControlText;
118             e.Graphics.DrawString(strRowNumber,
this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
119      
120         }
121
122         
private void txtProductname_TextChanged(object sender, EventArgs e)
123         {
124             
try
125             {
126                 con =
new SqlConnection(cs.DBConn);
127                 con.Open();
128                 cmd =
new SqlCommand("SELECT RTRIM(ProductID),RTRIM(ProductName),RTRIM(Category.ID),RTRIM(CategoryName),RTRIM(SubCategory.ID),RTRIM(SubCategoryName),RTRIM(Features),RTRIM(Price),Image from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID and ProductName like '" + txtProductname.Text + "%' order by Productname", con);
129                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
130                 dataGridView1.Rows.Clear();
131                 
while (rdr.Read() == true)
132                 {
133                     dataGridView1.Rows.Add(rdr[
0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6], rdr[7],rdr[8]);
134                 }
135                 con.Close();
136             }
137             
catch (Exception ex)
138             {
139                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
140             }
141         }
142
143         
private void txtCategory_TextChanged(object sender, EventArgs e)
144         {
145             
try
146             {
147                 con =
new SqlConnection(cs.DBConn);
148                 con.Open();
149                 cmd =
new SqlCommand("SELECT RTRIM(ProductID),RTRIM(ProductName),RTRIM(Category.ID),RTRIM(CategoryName),RTRIM(SubCategory.ID),RTRIM(SubCategoryName),RTRIM(Features),RTRIM(Price),Image from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID and CategoryName like '" + txtCategory.Text + "%' order by Productname", con);
150                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
151                 dataGridView1.Rows.Clear();
152                 
while (rdr.Read() == true)
153                 {
154                     dataGridView1.Rows.Add(rdr[
0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6], rdr[7],rdr[8]);
155                 }
156                 con.Close();
157             }
158             
catch (Exception ex)
159             {
160                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
161             }
162         }
163
164         
private void txtSubCategory_TextChanged(object sender, EventArgs e)
165         {
166             
try
167             {
168                 con =
new SqlConnection(cs.DBConn);
169                 con.Open();
170                 cmd =
new SqlCommand("SELECT RTRIM(ProductID),RTRIM(ProductName),RTRIM(Category.ID),RTRIM(CategoryName),RTRIM(SubCategory.ID),RTRIM(SubCategoryName),RTRIM(Features),RTRIM(Price),Image from Product,Category,SubCategory where Product.CategoryID=Category.ID and Product.SubCategoryID=SubCategory.ID and SubCategoryName like '" + txtSubCategory.Text + "%' order by Productname", con);
171                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
172                 dataGridView1.Rows.Clear();
173                 
while (rdr.Read() == true)
174                 {
175                     dataGridView1.Rows.Add(rdr[
0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5], rdr[6], rdr[7],rdr[8]);
176                 }
177                 con.Close();
178             }
179             
catch (Exception ex)
180             {
181                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
182             }
183         }
184
185         
186     }
187 }


Gõ tìm kiếm nhanh...